package com.lap.auth.adapter.driven.web.authentication;

import com.lap.auth.application.authentication.AuthQueryService;
import com.lap.auth.application.authentication.command.LoginCmd;
import com.lap.auth.application.authentication.command.RefreshCmd;
import com.lap.auth.application.authentication.dto.RefreshTokenDto;
import com.lap.auth.application.authentication.dto.TokenDto;
import com.lap.auth.application.authentication.dto.UserDto;
import com.lap.auth.shared.annotations.LoginLog;
import com.lap.context.starter.handler.UserHandler;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户鉴权", description = "提供用户登录，退出，验证")
@RequiredArgsConstructor
@RestController
@RequestMapping("api/v1/authentication")
public class AuthenticationController {

  private final AuthQueryService authQueryService;

  @Operation(summary = "用户登录")
  @LoginLog
  @PostMapping("signIn")
  public TokenDto login(@RequestBody @Valid LoginCmd cmd) {
    return authQueryService.login(cmd);
  }

  @Operation(summary = "Token刷新")
  @PostMapping("refresh")
  public RefreshTokenDto refresh(@RequestBody @Valid RefreshCmd cmd) {
    return authQueryService.refreshToken(cmd);
  }

  @Operation(summary = "Token验证")
  @GetMapping("token")
  public UserDto validateToken(@RequestParam String token) {
    return authQueryService.validateToken(token);
  }

  @Operation(summary = "用户退出")
  @PostMapping("signOut")
  public void logout() {
    UserHandler.getUserId();
  }
}
